package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

import webshop.data.Product;

/**
 * Create a new product and place it in the database.
 * @author Vighi & sindre.
 */
public class ProductAddServlet extends AbstractSuperServlet {
    @Resource
    UserTransaction tx;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();

        String productName = null;
        String productDescription = null;
        int productPrice = 0;
        int productStock = 0;
        String empty = request.getParameter(null);

        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        try {
            // Get paramters.
            productName = request.getParameter("productname");
            productDescription = request.getParameter("productdescription");
            productPrice = Integer.valueOf(request.getParameter("productprice"));
            productStock = Integer.valueOf(request.getParameter("productstock"));
        } catch (NumberFormatException nfe){
            out.println("Paramter Error!");
            out.println("Use only numbers and normal letters.");
        }
        try{

            out.println("<html><head><title>" + this.getClass().getName() + "</title></head><body>");
            out.println("<h1>Product Registration</h1>");

            // Checks fields.
            if (productName.equals(empty) == false || productDescription.equals(empty) == false ||
                    request.getParameter("productprice").equals(empty) == false ||
                    request.getParameter("productstock").equals(empty) == false)
            {

        tx.begin();
                //Creates new product based on input.
                Product pro1 = new Product(productName, productDescription, productPrice, productStock);
                em.persist(pro1);

                out.println("A product with the following information has been registerd:");
                out.println("<br/>");
                out.println("Name: " + productName + ", Description: " + productDescription + ", Price: " + productPrice + ", Stock: " + productStock);

        tx.commit();

            } else {
                out.println("All fields must be filled out!");
                out.println("<a href=registerproduct.html>RegisterProduct</a><br/>");
            }
        } catch (Exception e) {
            out.println("<pre>");
            e.printStackTrace(out);
            out.println("</pre>");
        } finally { 
            out.println("<hr/>");
            out.println("<p><a href=\"index.html\">Home</a></p>");

            // 5 sec redirect.
            out.println("<meta http-equiv=Refresh content=2;url=adminPage.html> ");
            out.println("<br/>");
            out.println("Redirect in 2 sec.");
            out.println("</body>");
            out.println("</html>");
            em.close();
    }
        out.close();
    }
}
